package lpl.com.xyd.test.manage.impl;

import com.alibaba.fastjson.JSONObject;
import lpl.com.xyd.test.manage.common.response.HttpResponse;
import lpl.com.xyd.test.manage.convert.UserInfoConvert;
import lpl.com.xyd.test.manage.domain.UserDomain;
import lpl.com.xyd.test.manage.facade.UserFacade;
import lpl.com.xyd.test.manage.factory.UserDomainFactory;
import lpl.com.xyd.test.manage.info.UserInfo;
import lpl.com.xyd.test.manage.order.*;
import lpl.com.xyd.test.manage.param.UserParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserFacadeImpl implements UserFacade {

    private final static Logger LOGGER = LoggerFactory.getLogger(UserFacadeImpl.class);

    @Autowired
    private UserDomainFactory userDomainFactory;

    @Override
    public HttpResponse<UserInfo> login(UserLoginOrder order) {

        return null;
    }

    @Transactional
    @Override
    public HttpResponse<UserInfo> addUser(UserNewOrder order) {
        LOGGER.info("新增用户");
        HttpResponse<UserInfo> response = new HttpResponse<>();
        UserParam param = new UserParam();
        BeanUtils.copyProperties(order,param);
        UserDomain domain = userDomainFactory.create(param);
        boolean res = domain.checkNewPassWord(order.getPassword(),order.getConfirmPassWord());
        if (!res){
            response.failfure();
            response.setMessage("两次输入密码不一致");
            return response;
        }
        domain.md5AndBase64Encode();
        boolean insert = userDomainFactory.store(domain);
        if (insert){
            response.successful();
            response.setMessage("新增成功");
        }else {
            response.failfure();
            response.setMessage("系统异常，稍后处理");
        }
        return response;
    }

    @Transactional
    @Override
    public HttpResponse<UserInfo> modifyPassWord(UserModifyPassWordOrder order) {
        LOGGER.info("用户密码修改");
        HttpResponse<UserInfo> response = new HttpResponse<>();
        UserDomain domain = userDomainFactory.active(order.getUserName());
        if (null == domain){
            response.failfure();
            response.setMessage("该用户不存在！");
            return response;
        }

        if (domain.checkPassWord(order.getOldPassWord())){
            response.failfure();
            response.setMessage("原密码不正确！");
            return response;
        }

        if (!domain.checkNewPassWord(order.getNewPassWord(),order.getConfirmPassWord())){
            response.failfure();
            response.setMessage("两次输入密码不一致！");
            return response;
        }
        domain.modifyPassWord(order.getNewPassWord());
        boolean insert = userDomainFactory.reStore(domain);
        if (!insert){
            response.failfure();
            response.setMessage("修改密码失败");
            return response;
        }
        response.successful();
        response.setMessage("修改密码成功");
        return response;
    }

    @Override
    public HttpResponse<List<UserInfo>> queryUser(UserQueryOrder order) {
        LOGGER.info("查询用户, order:{}", JSONObject.toJSONString(order));
        HttpResponse<List<UserInfo>> response = new HttpResponse<>();
        UserParam param = new UserParam();
        BeanUtils.copyProperties(order,param);
        List<UserDomain> domains = userDomainFactory.activeList(param);
        if (null == domains || domains.size() <= 0){
            response.failfure();
            response.setMessage("没有数据");
            return response;
        }
        List<UserInfo> userInfos = new ArrayList<>(domains.size());
        domains.forEach(v->{
            UserInfo info = UserInfoConvert.convert(v);
            userInfos.add(info);
        });
        response.setData(userInfos);
        return response;
    }

    @Override
    public HttpResponse<UserInfo> delUser(UserDelOrder order) {
        LOGGER.info("删除用户, order:{}", JSONObject.toJSONString(order));
        HttpResponse<UserInfo> response = new HttpResponse<>();
        boolean result = userDomainFactory.delete(order.getId());
        if (!result){
            response.failfure();
            response.setMessage("系统异常，请稍后重试");
        }
        response.successful();
        response.setMessage("删除用户成功");
        return response;
    }
}
